
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: DoubleField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.dellroad.stuff.string.DoubleFormat;
import org.dellroad.stuff.string.ParseContext;

/**
 * Double field.
 *
 * @see org.dellroad.sidekar.annotation.SidekarField
 */
public final class DoubleField extends AtomicField<Double> {

    DoubleField(String name, Entity<?> entity) {
        super(name, entity, Double.class);
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseDoubleField(this);
    }

    @Override
    public void encode(DataOutputStream output, Double value) throws IOException {
        output.writeDouble(value);
    }

    @Override
    public Double decode(DataInputStream input) throws IOException {
        return input.readDouble();
    }

    @Override
    public String toString(Double value) {
        return value.toString();
    }

    @Override
    public Double fromString(ParseContext ctx) {
        return Double.parseDouble(ctx.matchPrefix(DoubleFormat.PATTERN).group());
    }
}

